<!--  线 + 柱混合图 -->
<template>
  
  <!--   <template #header>
      <div class="title">
        业绩柱状图
        <el-tooltip effect="dark" content="点击试试下载" placement="bottom">
          <i-ep-download class="download" @click="downloadEchart" />
        </el-tooltip>
      </div>
    </template> -->

    <div :id="id" :class="className" :style="{ height, width }"></div>

</template>

<script setup lang="ts">
import * as echarts from "echarts";

const props = defineProps({
  id: {
    type: String,
    default: "barChart",
  },
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "90%",
    required: true,
  },
  height: {
    type: String,
    default: "280px",
    required: true,
  },
});

const options = {
  tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          show: false
        },
        grid: {
          left: '15%',
          right: '4%',
          // bottom: '3%',
         // containLabel: true
         borderWidth: 0, // 将边框宽度设置为 0
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01],
          axisLabel: {
            color: 'rgba(153, 153, 153, 1)' // 设置x轴标签颜色为灰色
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(153, 153, 153, 1)' // 设置x轴线条颜色为灰色
            }
          }
        },
        yAxis: {
          type: 'category',
          data: ['昨日代付', '今日代付'],
          axisLabel: {
            color: 'rgba(153, 153, 153, 1)' // 设置x轴标签颜色为灰色
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(153, 153, 153, 1)' // 设置x轴线条颜色为灰色
            }
          }
        },
        series: [
          {
            name: '2011',
            type: 'bar',
            data: [18203, 23489],
            barGap: 0, // 设置柱条间的间隙为0
            itemStyle: {
              color: 'rgba(32, 141, 255, 1)' // 设置第一个条形图的颜色为蓝色
            }
          },
          {
            name: '2012',
            type: 'bar',
            data: [19325, 23438],
            barGap: 0, // 设置柱条间的间隙为0
            itemStyle: {
              color: 'rgba(20, 201, 201, 1)' // 设置第一个条形图的颜色为蓝色
            }
          }
        ]
  
};
const chart = ref<any>("");
onMounted(() => {
  // 图表初始化
  chart.value = markRaw(
    echarts.init(document.getElementById(props.id) as HTMLDivElement)
  );

  chart.value.setOption(options);

  // 大小自适应
  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});
const downloadEchart = () => {
  // 获取画布图表地址信息
  const img = new Image();
  img.src = chart.value.getDataURL({
    type: "png",
    pixelRatio: 1,
    backgroundColor: "#fff",
  });
  // 当图片加载完成后，生成 URL 并下载
  img.onload = () => {
    const canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    const ctx = canvas.getContext("2d");
    if (ctx) {
      ctx.drawImage(img, 0, 0, img.width, img.height);
      const link = document.createElement("a");
      link.download = `业绩柱状图.png`;
      link.href = canvas.toDataURL("image/png", 0.9);
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
  };
};
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  justify-content: space-between;

  .download {
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}
</style>
